package cn.ym.mall.service.impl;

import cn.ym.mall.dao.CommentDao;
import cn.ym.mall.dao.impl.CommentDaoImpl;
import cn.ym.mall.entity.Comment;
import cn.ym.mall.entity.Complaints;
import cn.ym.mall.service.CommentService;
import cn.ym.mall.utils.DruidUtils;
import cn.ym.mall.vo.Page;

import java.sql.Connection;
import java.sql.SQLException;

public class CommentServiceImpl implements CommentService {
    private CommentDao commentDao = new CommentDaoImpl();

    /**
     * 通过商品查询评论
     *
     * @param gid
     * @param pageNo
     * @param pageSize
     * @return
     */
    @Override
    public Page<Comment> getCommentByGid(Integer gid, Integer pageNo, Integer pageSize) throws SQLException {
        Connection conn = null;
        Page<Comment> page = new Page<>();
        try {
            conn = DruidUtils.getConnection();
            page.setPageNo(pageNo);
            page.setPageSize(pageSize);
            page.setCount((int) commentDao.getCountByGid(gid));
            page.setList(commentDao.getCommentByGid(gid,pageNo,pageSize));
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DruidUtils.close();
        }
        return page;
    }

    /**
     * 通过用户查询评论
     *
     * @param uid
     * @param pageNo
     * @param pageSize
     * @return
     */
    @Override
    public Page<Comment> getCommentByUid(Integer uid, Integer pageNo, Integer pageSize) throws SQLException {
        Connection conn = null;
        Page<Comment> page = new Page<>();
        try {
            conn = DruidUtils.getConnection();
            page.setPageNo(pageNo);
            page.setPageSize(pageSize);
            page.setCount((int) commentDao.getCountByUid(uid));
            page.setList(commentDao.getCommentByUid(uid,pageNo,pageSize));
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DruidUtils.close();
        }
        return page;
    }

    /**
     * 添加评论
     *
     * @param comment
     * @return
     */
    @Override
    public boolean add(Comment comment) throws SQLException {
        Connection conn = null;
        try {
            conn = DruidUtils.getConnection(false);
            if (commentDao.add(comment) == 1) {
                DruidUtils.commit();
                return true;
            }else {
                DruidUtils.rollback();
            }
        } catch (Exception e) {
            DruidUtils.rollback();
            e.printStackTrace();
        } finally {
            DruidUtils.close();
        }
        return false;
    }

    /**
     * 删除评论
     *
     * @param cid
     * @return
     */
    @Override
    public boolean delete(Integer cid) throws SQLException {
        Connection conn = null;
        try {
            conn = DruidUtils.getConnection(false);
            if (commentDao.delete(cid) == 1) {
                DruidUtils.commit();
                return true;
            }else {
                DruidUtils.rollback();
            }
        } catch (Exception e) {
            DruidUtils.rollback();
            e.printStackTrace();
        } finally {
            DruidUtils.close();
        }
        return false;
    }

    /**
     * 删除商品所有评论
     *
     * @param gid
     * @return
     * @throws SQLException
     */
    @Override
    public boolean deleteByGid(Integer gid) throws SQLException {
        Connection conn = null;
        try {
            conn = DruidUtils.getConnection(false);
            if (commentDao.deleteByGid(gid) == commentDao.getCountByGid(gid)) {
                DruidUtils.commit();
                return true;
            }else {
                DruidUtils.rollback();
            }
        } catch (Exception e) {
            DruidUtils.rollback();
            e.printStackTrace();
        } finally {
            DruidUtils.close();
        }
        return false;
    }

}
